#include <iostream>
using std::cout;
using std::endl;

class Base{
public:
    Base(){
        cout << "Base()" << endl;
    } 

    Base(long base)
    : _base(base)
    { cout << "Base(long)" << endl; }

    virtual void print(){
        cout << "hello,world" << endl;
    }

    ~Base(){ cout << "~Base()" << endl; }
private:
    long _base;
};



class Derived
: public Base
{
public:
    Derived(){ cout << "Derived()" << endl; }

    Derived(long a,long c)
    : Base(a) //调用基类的构造，写的是基类的类名
    , _derived(c)
    {
        cout << "Derived(long * 3)" << endl;
    }
    ~Derived(){ cout << "~Derived()" << endl; }
private:
    long _derived;
};

void test0(){
    Base base;
    Derived d1;

    //Base & operator=(const Base & rhs)
    //参数中的const还有一个作用，就是可以接收不同类型的参数
    //const Base & rhs = d1
    //
    //统称为向上转型，是可行的
    base = d1;//ok
    const Base & rhs = d1;//ok
    Base * pbase = &d1;//ok
    pbase = &base;
#if 0
    //向下转型
    //这个是合理的
    Derived * pd = (Derived*)pbase;

    pbase = &base;
    //这个是不合理的
    Derived * pd2 = (Derived*)pbase;
    //希望有一个方法自动判断，向下转型是否合理
#endif    
    Derived * pd = dynamic_cast<Derived*>(pbase);
    if(pd == nullptr){
        cout << "转换失败" << endl;
    }else{
        cout << "转换成功" << endl;
        pd->print();
    }



    /* d1 = base;//error */
    /* Derived * pderived = &base; */
    /* const Derived & rhs = base; */
}

int main(void){
    test0();
    return 0;
}
